package edu.neusoft.android.yuejian14.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import java.io.File;

import edu.neusoft.android.yuejian14.R;

public class AddPicActivity extends BaseActivity {
    private ImageButton back;
    private EditText  Content;
    private Button Submit;

    //新加入的
    private ImageView mImage;
    private ImageButton mAddImage;
    private Bitmap mBitmap;
    protected static final int CHOOSE_PICTURE=0;
    protected static final int TAKE_PICTURE=1;
    protected static Uri tempUri;
    private static final int CROP_SMALL_PICTURE=2;

    @Override
    void initViews() {

    }

    @Override
    View initViews(View view) {
        return null;
    }

    @Override
    void initEvents() {

    }

    @Override
    void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_pic);
        back=(ImageButton)findViewById(R.id.btn_back);
        Content=(EditText)findViewById(R.id.tab_wmb_Content);
        Submit=(Button)findViewById(R.id.tab_wmb_sendmb);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
        //新加入的
        initUI();
        initListener();
    }
    private void initUI(){
        mImage=(ImageView)findViewById(R.id.imageview);
        mAddImage=(ImageButton)findViewById(R.id.buttonLoadPicture);
    }
    private void initListener(){
        mAddImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChoosePicDialog();
            }
        });
    }
//显示修改图片的对话框
    protected void showChoosePicDialog(){
        AlertDialog.Builder builder=new AlertDialog.Builder(AddPicActivity.this);
        builder.setTitle("添加图片");
        String[] items={"选择本地图片","拍照"};
        builder.setNegativeButton("取消",null);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case CHOOSE_PICTURE://选择本地照片
                        Intent openAlbumIntent=new Intent(
                                Intent.ACTION_GET_CONTENT);
                        openAlbumIntent.setType("image/*");
                        startActivityForResult(openAlbumIntent,CHOOSE_PICTURE);
                        break;
                    case TAKE_PICTURE://拍照
                        Intent openCameraIntent=new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);
                        tempUri=Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"temp_image.jpg"));
                        startActivityForResult(openCameraIntent,TAKE_PICTURE);
                        break;
                }
            }
        });
        builder.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== AddPicActivity.RESULT_OK){
            switch (requestCode){
                case TAKE_PICTURE:
                    cutImage(tempUri);//对图片进行裁剪处理
                    break;
                case CHOOSE_PICTURE:
                    cutImage(data.getData());
                    break;
                case CROP_SMALL_PICTURE:
                    if (data!=null){
                        setImageToView(data);
                    }
                    break;
            }
        }
    }
    //裁剪图片的方法
    protected void cutImage(Uri uri){
        if (uri==null){
            Log.i("alanjet","The uri is not exist");
        }
        tempUri=uri;
        Intent intent=new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri,"image/*");
        intent.putExtra("crop","true");
        intent.putExtra("aspectX",1);
        intent.putExtra("aspectY",1);

        intent.putExtra("outputX",150);
        intent.putExtra("outputY",150);
        intent.putExtra("return-data",true);
        startActivityForResult(intent,CROP_SMALL_PICTURE);
    }
    protected void setImageToView(Intent data){
        Bundle extras=data.getExtras();
        if (extras!=null){
            mBitmap=extras.getParcelable("data");
            mImage.setImageBitmap(mBitmap);
        }
    }
}
